package com.realtyManage.springboot.Controller;

import com.realtyManage.springboot.Model.Do.CustomerRegistration;
import com.realtyManage.springboot.Model.Do.HouseDetail;
import com.realtyManage.springboot.Model.Vo.*;
import com.realtyManage.springboot.Service.*;
import com.realtyManage.springboot.Utils.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ExportController
 *
 * @Author 杭州素色网络科技有限公司
 * @Date 2019/7/19
 * 导出数据
 **/

@Controller
@RequestMapping("/export")
public class ExportController {
    private final Logger log = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private IExportDataService exportDataService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IShopService shopService;
    @Autowired
    private IHouseDetailService houseDetailService;
    @Autowired
    private ICustomerRegistrationService customerRegistrationService;


    @RequestMapping(value = "/data", method = {RequestMethod.GET,RequestMethod.POST})
    public void exportGroupAssessData(ExportEntity exportEntity, HttpServletResponse response, HttpServletRequest request, HouseEntity houseEntity) {
        //xls格式最大存储行数
        Integer max = 65535;
        List<HouseDetail> list = new ArrayList<>();
        List<String> header = new ArrayList<>();
        List<String> field = new ArrayList<>();
     //   list = houseDetailService.findPagedByWhereClause(houseEntity).getContent();
        switch (exportEntity.getExportType()){
            case "1":
                list = houseDetailService.findPagedByWhereClause(houseEntity).getContent();
                //excel表格头
                header.add("幢号");
                header.add("单元号");
                header.add("门牌号");
                header.add("建筑面积");
                header.add("优惠价");
                header.add("优惠总价");
                header.add("朝向");
                header.add("套内面积");
                header.add("原单价");
                header.add("原总价");
                header.add("状态");

                header.add("户型编号");
                header.add("户型结构");
                header.add("楼层");
                header.add("套型");
                header.add("物业类型");
                header.add("实际使用面积");
                header.add("庭院面积");
                header.add("公摊面积");

                //对应实体字段
                field.add("blockCode");
                field.add("unitCode");
                field.add("doorplateCode");
                field.add("architectureArea");
                field.add("discountPrice");
                field.add("discountTotal");
                field.add("face");
                field.add("indoorArea");
                field.add("oldPrice");
                field.add("oldTotal");
                field.add("type");

                field.add("apartmentNumber");
                field.add("apartmentStructure");
                field.add("floor");
                field.add("jacketed");
                field.add("propertyTye");
                field.add("actualUseArea");
                field.add("courtyardArea");
                field.add("publicArea");
                break;
        }
        HSSFWorkbook xssfSheets = ExcelUtil.exportExcel(list, header.toArray(new String[0]), field.toArray(new String[0]),"价格表信息");
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        OutputStream outputStream = null;
        StringBuilder sb = new StringBuilder();
        try {
            xssfSheets.write(os);
            byte[] bytes = os.toByteArray();
            response.reset();
            response.setContentType("application/ms-excel;charset=UTF-8");
            sb.append("价格表信息");
            sb.append(".xls");
            String filename = URLEncoder.encode(sb.toString(), "utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + filename);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            outputStream = response.getOutputStream();
            outputStream.write(bytes);
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != outputStream) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * 导出客户信息
     * @param customReEntity
     * @param response
     * @param request
     */
    @RequestMapping(value = "/exportCustomerData", method = {RequestMethod.GET,RequestMethod.POST})
    public void exportCustomerData(CustomReEntity customReEntity, HttpServletResponse response, HttpServletRequest request) {
        //xls格式最大存储行数
        Integer max = 65535;
        customReEntity.setPageNumber(1);
        customReEntity.setPageSize(max);
        List<CustomerRegistration> list = new ArrayList<>();
        List<String> header = new ArrayList<>();
        List<String> field = new ArrayList<>();
                list = customerRegistrationService.findPagedByWhereClause(customReEntity).getContent();
                //excel表格头
                header.add("姓名");
                header.add("电话");
                header.add("置业次数");
                header.add("客户状态");
                header.add("已回款");
                header.add("置业顾问");

                //对应实体字段
                field.add("name");
                field.add("phone");
                field.add("homeOwnership");
                field.add("customerType");
                field.add("moneyBack");
                field.add("receptionName");

        HSSFWorkbook xssfSheets = ExcelUtil.exportCustomerExcel(list, header.toArray(new String[0]), field.toArray(new String[0]),"客户表信息");
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        OutputStream outputStream = null;
        StringBuilder sb = new StringBuilder();
        try {
            xssfSheets.write(os);
            byte[] bytes = os.toByteArray();
            response.reset();
            response.setContentType("application/ms-excel;charset=UTF-8");
            sb.append("客户表信息");
            sb.append(".xls");
            String filename = URLEncoder.encode(sb.toString(), "utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + filename);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            outputStream = response.getOutputStream();
            outputStream.write(bytes);
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != outputStream) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
